<?php
namespace app\index\controller;

use app\index\model\User;
use app\index\model\Breaktable;
use app\index\model\Hyy;
use think\Db;
use think\Request;

class IndexController extends \think\Controller
{
    public function index(Request $request)
    {
//        header('Access-Control-Allow-Origin:*');
        dump(111);die;
    }

    //管理员添加用户的方法
    public function hello(Request $request){
//        header('Access-Control-Allow-Origin:*');
//        print_r($request->post());
//        var_dump($request->post());
//        $postData = $request->post();
//        if($postData['xm']== 'yyy')
//        {
//            echo "等于";
//        }else{
//            echo '不等于';
//        }

        $postData = $request->post();
//        dump($postData)
//        $postData['addtime'] = date('Y-m-d H:i:s');
        $postData['addtime'] = date('Y-m-d H:i:s');
        $postData['password'] = md5($postData['password']);
//        dump($postData);die;
//        $user = new User();
        $rst = Db::table('user')->insert($postData);
//        $rst = $user->save($postData);
//        dump($postData);
        if($rst == 1){
//            echo "添加数据成功";
            return '添加用户成功';
        }
//        if($rst == 1){
//            return "添加数据成功";
//        }else{
//            return "添加数据失败";
//        }

    }


    //故障发布触发站点名称事件处理方法
    public function zdmcsj(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $data = $request->post('value');
//        $bstaion = Db::table('bstaion')->where('zdmc',$data)->find();
//        $bstaion_id =  $bstaion['id'];
//        $jizhanguanlian = Db::table('jizhanguanlian')->where('jizhanid',$bstaion_id)->select();
//        var_dump($jizhanguanlian);

//        $arr = [];
//        foreach($jizhanguanlian as $k => $v){
//            array_push($arr,$v['gxqk']);
//        }
//        var_dump($arr);
        $zddz = Db::table('bstaion')->where('zdmc',$data)->find();
        $zddz = $zddz['dz'];
        if($zddz == ''){
            return 1;
        }
        return $zddz;
    }

    //故障发布触发站点名称事件处理运营商显示信息
    public function yysxs(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $data = $request->post('value');
        $bstaion = Db::table('bstaion')->where('zdmc',$data)->find();
        $bstaion_id =  $bstaion['id'];
        $jizhanguanlian = Db::table('jizhanguanlian')->where('jizhanid',$bstaion_id)->select();
//        var_dump($jizhanguanlian);

        $arr = [];
        foreach($jizhanguanlian as $k => $v){
            array_push($arr,$v['gxqk']);
        }
        return json($arr);
    }

    //故障发布触发站点名称事件处理运营商显示信息
    public function yysxsz(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $data = $request->post('value');
        $bstaion = Db::table('bstaion')->where('zdmc',$data)->find();
        $bstaion_id =  $bstaion['id'];
        $jizhanguanlian = Db::table('jizhanguanlian')->where('jizhanid',$bstaion_id)->select();
//        var_dump($jizhanguanlian);

        $arr = [];
        foreach($jizhanguanlian as $k => $v){
            array_push($arr,$v['gxqk']);
        }
        return json($arr);
    }

    //管理员故障发布的方法
    public function guzhang(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $postData = $request->post();
//       var_dump($postData);die;
//        array(5) { ["site_name"]=> string(0) "" ["site_address"]=> string(0) "" ["fault_content"]=> string(0) "" ["fault_type"]=> string(6) "鏂數" ["work_order"]=> string(1) "A" }
//        $data = new Hyy();
//        $shuju = [
//            "fault_number"=>'GUZ'. time(),
//            "site_name"=>$postData.site_name,
//            "site_address"=>$postData.site_address,
//            "fault_content"=>$postData.fault_content,
//            "fault_type"=>$postData.fault_type,
//            "work_order"=>$postData.work_order
//        ];

//        $shuju = [
//            "fault_number"=>'GUZ'. time(),
//        ];
//        array_push($postData,$shuju);
//
////        dump($postData);
//       $rst = $data->save($postData);
//        if($rst == 1){
//            echo "添加数据成功";
//        }

//        array_push($postData);
//        array_merge(arrat('fault_number'=>'GUZ'+ time()),$postData);

        $postData['fault_number'] = 'GDB'.time().GetRandStr(3);
        $postData['adduid'] = 4;
        $postData['addname'] = "管理员";
        $postData['addtime'] = date('Y-m-d H:i:s');
//        $postData['addtime'] = time();
        $rst = Db::table('breaktable')->insert($postData);
        if($rst == 1){
            return "故障发布成功";
        }else{
            return "发布失败";
        }
//        return '哈哈哈';
    }

    //用户提交登陆的方法
    public function login(Request $request){
//        header('Access-Control-Allow-Origin:*');
        //接受前端用户提交过来的数据
        $postDate = $request->post();
//        dump($postDate);
        $account = $postDate['account'];     //用户提交的账号
        $password = $postDate['password'];  //用户提交的密码
        $password = md5($password);
        //查询用户数据库中的数据是否跟前端用户提交过来的数据一样
        $name =  Db::table('user')->where('account',$account)->find();
//        $xingming = $name['name'];     //获取到用户登陆的姓名
        //如果一样进行登陆并跳转到首页
//        dump($name);die;
        if($name['account'] == $account && $name['password'] == $password){
//            return ['data'=>'登陆成功','account'=>$account,'name'=>$name['name']];
            $arr = array(
                'data'=>'登陆成功',
                'account'=>$name['account'],
                'name'=>$name['name'],
                'uid'=>$name['id'],
                'jurisdiction'=>$name['jurisdiction'],
                'mobile'=>$name['mobile']
            );
//            $json = json($arr);
            return json($arr);
        }
        if($name['account'] == $account && $name['password'] !== $password){
            return "密码错误";
        }
        if($name['account'] !== $account){
            return "账号不对";
        }
        //如果不一样进行跳转重新登陆

//        return 123;
    }


    //自己的方法
    public function hyy(){
//        header('Access-Control-Allow-Origin:*');
        return 'GUZ'.time().GetRandStr(6);
//        $a = "/upload/image/952152255.png";
//        "/upload/image/952152255.png".split(',');
    }

    //添加抄表工单处理方法
    public function mreading(Request $request){
//        header('Access-Control-Allow-Origin:*');
        //1、先查询处基站表中的10条基站信息
//        return 555;
        $jizhan = Db::table('bstaion')->select();
//        var_dump($jizhan);die;
        //2、需要循环遍历对抄表的字段的添加
        foreach ($jizhan as $k => $v) {

//            echo $k . '<br>';
//            print_r($v);
//            echo '<br>';

//            dump($v['quxian']);
            Db::table('mreading')->insert(['mread_number'=>'CDB'.time().GetRandStr(3),'site_name'=>$v['zdmc'],'site_area'=>$v['quxian'],'site_address'=>$v['dz'],'adduid'=>4,'addname'=>'管理员','addtime'=>date('Y-m-d H:i:s'),'state'=>'1']);

//            foreach ($jizhan[$k] as $index => $value) {
//
//                echo $k . '<br>';
//
//                echo $index . '<br>';
//
//                echo $value . '<br>';
//
//                //开始写自己的组装拼接流程
//
//
//
//            }

        }
        //3、添加数据表数据
        return "抄表发布成功";
    }

    //添加巡检工单发布处理方法
    public function inspection(){
//        header('Access-Control-Allow-Origin:*');
        //先查询出来基站的所有数据
        $jizhan = Db::table('bstaion')->select();

        foreach ($jizhan as $k => $v) {

//            echo $k . '<br>';
//            print_r($v);
//            echo '<br>';
            Db::table('inspection')->insert(['inspe_number'=>'XDB'.time().GetRandStr(3),'site_name'=>$v['zdmc'],'site_area'=>$v['quxian'],'site_address'=>$v['dz'],'adduid'=>4,'addname'=>'管理员','addtime'=>date('Y-m-d H:i:s'),'state'=>'1']);

        }
           return "巡检发布成功";
    }

    //发布施工工单处理方法
    public function constr(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $postData = $request->post();
//        var_dump($postData);die;

        $postData['con_number'] = 'SDB'.time().GetRandStr(3);
        $postData['adduid'] = 4;
        $postData['addname'] = "管理员";
        $postData['addtime'] = date('Y-m-d H:i:s');
        $rst = Db::table('construction')->insert($postData);
        if($rst == 1){
            return "施工发布成功";
        }else{
            return "发布失败";
        }

    }

    //故障审核返回列表处理方法
    public function gzsh(){
//        header('Access-Control-Allow-Origin:*');
        $data = Db::table('breaktable')->select();
//        var_dump($data);die;
//        $count = count($data);
//        $data = Db::table('breaktable')->paginate(5,$count);
//        return json($data);
        $arr = [];
        foreach ($data as $k => $v) {

//            echo $k . '<br>';
//            print_r($v);
//            echo '<br>';
            if($v['state'] == '3'){
                array_push($arr,$v);
            }
//            array_push($arr,$v);
//            return json($v);
        }
//        var_dump($arr[8]);die;

//        $arr = array(
//            'fault_number'=>$data[0]['fault_number'],
//            'site_name'=>$data[0]['site_name'],
//            'addtime'=>$data[0]['addtime'],
//            'addname'=>$data[0]['addname']
//        );
//////            $json = json($arr);
        return json($arr);
    }


    //抄表工单审核列表显示方法
    public function meter(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $data = Db::table('mreading')->select();
        $arr = [];
        foreach ($data as $k => $v) {
            if($v['state'] == '3'){
                array_push($arr,$v);
            }
        }
        return json($arr);
    }


    //审核巡检工单列表显示处理方法
    public function inspect(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $data = Db::table('inspection')->select();
        $arr = [];
        foreach ($data as $k => $v) {
            if($v['state'] == '3'){
                array_push($arr,$v);
            }
        }
        return json($arr);
    }

    //审核施工工单列表显示处理方法
    public function cons(){
//        header('Access-Control-Allow-Origin:*');
        $data = Db::table('construction')->select();
        $arr = [];
        foreach ($data as $k => $v) {
            if($v['state'] == '3'){
                array_push($arr,$v);
            }
        }
        return json($arr);
    }

    //故障工单审核详情页面的处理方法
    public function gzshxq(Request $request){
//        header('Access-Control-Allow-Origin:*');
        //接收到前端页面传递过来的工单号数据
        $data = $request->post();
//        $frist = substr( $data['gdh'], 0, 1 );
//        $delete_last = substr($frist,0,-1);
        $newstr = substr($data['gdh'],0,strlen($data['gdh'])-1);
        $data = substr($newstr,1);
//        var_dump($data);die;
        //拿到工单号数据进行对数据库查找
        $shuju = Db::table('breaktable')->where('fault_number',$data)->find();
        $site_name = $shuju['site_name'];
        $uid = $shuju['uid'];  //用户表的id号
        $user = Db::table('user')->where('id',$uid)->find();

        $bstaion =  Db::table('bstaion')->where('zdmc',$site_name)->find();
        $jizhanguanlian_id = $bstaion['id'];
        $jizhanguanlian = Db::table('jizhanguanlian')->where('jizhanid',$jizhanguanlian_id)->select();

        //进行判断处理
        //如果前端提交啥运营商，那么我这边就只回传那个运营商的信息
//        $yys = $shuju['operators'];
//        var_dump($yys);die;
        //得到运营商数据之后，我们进行回传

        foreach ($jizhanguanlian as $k => $v) {
            $jizhanguanlian[$v['gxqk']] = [
                $v['gxqk'] =>$v,
            ];
            unset($jizhanguanlian[$k]);
        }


//        die;
//        $jizhanguanlian = [
//            '移动' =>$jizhanguanlian[0],
//            '联通' =>$jizhanguanlian[1],
//            '电信' =>$jizhanguanlian[2],
//        ];
//        $jizhanguanlian[0] = [
//            '移动' =>$jizhanguanlian[0],
//        ];
//        var_dump($jizhanguanlian); die;

//        if($jizhanguanlian[0] !== '' ){
//            $jizhanguanlian['移动'] = $jizhanguanlian[0];
//            unset($jizhanguanlian[0]);
//        }
//        if($jizhanguanlian[1] !== '' ){
//            $jizhanguanlian['联通'] = $jizhanguanlian[1];
//            unset($jizhanguanlian[1]);
//        }
//        if($jizhanguanlian[2]){
//            $jizhanguanlian['电信'] = $jizhanguanlian[2];
//            unset($jizhanguanlian[2]);
//        }

//        var_dump($jizhanguanlian);die;
        array_push($shuju,$user);
        array_push($shuju,$bstaion);
        array_push($shuju,$jizhanguanlian);

        return json($shuju);
    }

    //故障审核详情页面提交完成的处理方法
    public function gzshxqwc(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $postdata = $request->post();
        $rst =  Db::table('breaktable')->where('fault_number',$postdata['fault_number'])->update(['state'=>4]);
        if($rst == 1){
            return "审核完成";
        }else{
            return "审核失败";
        }
//        var_dump($postdata);
    }

    //抄表工单审核详情页面的处理方法
    public function cbshxq(Request $request){
//        header('Access-Control-Allow-Origin:*');
        //接收到前端页面传递过来的工单号数据
        $data = $request->post();
//        $frist = substr( $data['gdh'], 0, 1 );
//        $delete_last = substr($frist,0,-1);
        $newstr = substr($data['gdh'],0,strlen($data['gdh'])-1);
        $data = substr($newstr,1);
//        var_dump($data);die;
        //拿到工单号数据进行对数据库查找
        $shuju = Db::table('mreading')->where('mread_number',$data)->find();
        $site_name = $shuju['site_name'];
        $uid = $shuju['uid'];  //用户表的id号
        $user = Db::table('user')->where('id',$uid)->find();

        $bstaion =  Db::table('bstaion')->where('zdmc',$site_name)->find();
        $jizhanguanlian_id = $bstaion['id'];
        $jizhanguanlian = Db::table('jizhanguanlian')->where('jizhanid',$jizhanguanlian_id)->select();
//        $yidong = [
//            'yidong_img'=>$shuju['yidong_img'],
//        ];
//        $liantong = [
//            'liantong_img'=>$shuju['liantong_img'],
//        ];
//        $dianxin = [
//            'dianxin_img'=>$shuju['dianxin_img'],
//        ];
//        array_push($jizhanguanlian[0],$shuju);
//        array_push($jizhanguanlian[1],$shuju);
//        array_push($jizhanguanlian[2],$shuju);

//        $jizhanguanlian[0] = [
//            'yidong_img' =>$shuju['yidong_img'],
//            'liantong_img' =>$shuju['liantong_img'],
//            'dianxin_img' =>$shuju['dianxin_img'],
//        ];

//        array_push($jizhanguanlian[0],$shuju['yidong_img']);
//        array_push($jizhanguanlian[1],$shuju['liantong_img']);
//        array_push($jizhanguanlian[2],$shuju['dianxin_img']);


//        $jizhanguanlian = [
//            '移动' =>$jizhanguanlian[0],
//            '联通' =>$jizhanguanlian[1],
//            '电信' =>$jizhanguanlian[2],
//        ];

//        var_dump($jizhanguanlian);die;
        array_push($shuju,$user);
        array_push($shuju,$bstaion);
        array_push($shuju,$jizhanguanlian);

        return json($shuju);
    }


    //抄表审核详情页面提交完成的处理方法
    public function cbshxqwc(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $postdata = $request->post();
        $rst =  Db::table('mreading')->where('mread_number',$postdata['mread_number'])->update(['state'=>4]);
        if($rst == 1){
            return "审核完成";
        }else{
            return "审核失败";
        }
//        var_dump($postdata);
    }


    //施工工单审核详情页面的处理方法
    public function sgshxq(Request $request){
//        header('Access-Control-Allow-Origin:*');
        //接收到前端页面传递过来的工单号数据
        $data = $request->post();
//        $frist = substr( $data['gdh'], 0, 1 );
//        $delete_last = substr($frist,0,-1);
        $newstr = substr($data['gdh'],0,strlen($data['gdh'])-1);
        $data = substr($newstr,1);
//        var_dump($data);die;
        //拿到工单号数据进行对数据库查找
        $shuju = Db::table('construction')->where('con_number',$data)->find();
        $site_name = $shuju['site_name'];
        $uid = $shuju['uid'];  //用户表的id号
        $user = Db::table('user')->where('id',$uid)->find();

        $bstaion =  Db::table('bstaion')->where('zdmc',$site_name)->find();
        $jizhanguanlian_id = $bstaion['id'];
        $jizhanguanlian = Db::table('jizhanguanlian')->where('jizhanid',$jizhanguanlian_id)->select();
//        $jizhanguanlian = [
//            '移动' =>$jizhanguanlian[0],
//            '联通' =>$jizhanguanlian[1],
//            '电信' =>$jizhanguanlian[2],
//        ];


        foreach ($jizhanguanlian as $k => $v) {
            $jizhanguanlian[$v['gxqk']] = [
                $v['gxqk'] =>$v,
            ];
            unset($jizhanguanlian[$k]);
        }

//        var_dump($jizhanguanlian);die;
        array_push($shuju,$user);
        array_push($shuju,$bstaion);
        array_push($shuju,$jizhanguanlian);

        return json($shuju);
    }


    //施工审核详情页面提交完成的处理方法
    public function sgshxqwc(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $postdata = $request->post();
        $rst =  Db::table('construction')->where('con_number',$postdata['con_number'])->update(['state'=>4]);
        if($rst == 1){
            return "审核完成";
        }else{
            return "审核失败";
        }
//        var_dump($postdata);
    }



    //巡检工单审核详情页面的处理方法
    public function xjshxq(Request $request){
//        header('Access-Control-Allow-Origin:*');
        //接收到前端页面传递过来的工单号数据
        $data = $request->post();
//        $frist = substr( $data['gdh'], 0, 1 );
//        $delete_last = substr($frist,0,-1);
        $newstr = substr($data['gdh'],0,strlen($data['gdh'])-1);
        $data = substr($newstr,1);
//        var_dump($data);die;
        //拿到工单号数据进行对数据库查找
        $shuju = Db::table('inspection')->where('inspe_number',$data)->find();
        $site_name = $shuju['site_name'];
        $uid = $shuju['uid'];  //用户表的id号
        $user = Db::table('user')->where('id',$uid)->find();

        $bstaion =  Db::table('bstaion')->where('zdmc',$site_name)->find();
        $jizhanguanlian_id = $bstaion['id'];
        $jizhanguanlian = Db::table('jizhanguanlian')->where('jizhanid',$jizhanguanlian_id)->select();
//        $jizhanguanlian = [
//            '移动' =>$jizhanguanlian[0],
//            '联通' =>$jizhanguanlian[1],
//            '电信' =>$jizhanguanlian[2],
//        ];


        foreach ($jizhanguanlian as $k => $v) {
            $jizhanguanlian[$v['gxqk']] = [
                $v['gxqk'] =>$v,
            ];
            unset($jizhanguanlian[$k]);
        }



//        var_dump($jizhanguanlian);die;
        array_push($shuju,$user);
        array_push($shuju,$bstaion);
        array_push($shuju,$jizhanguanlian);

        return json($shuju);
    }


    //巡检审核详情页面提交完成的处理方法
    public function xjshxqwc(Request $request){
//        header('Access-Control-Allow-Origin:*');
        $postdata = $request->post();
        $rst =  Db::table('inspection')->where('inspe_number',$postdata['inspe_number'])->update(['state'=>4]);
        if($rst == 1){
            return "审核完成";
        }else{
            return "审核失败";
        }
//        var_dump($postdata);
    }
}

